package com.cn.httptest;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;

public class HttpTest1 {

	public static void main(String[] args) throws Exception {

		int dk = 10;
		dk = dk >> 1;
		System.out.println(dk);

	}

	public static void proxy() throws MalformedURLException, IOException, ProtocolException {
		URL url = new URL("http://www.cnblogs.com/caiwenz/p/3913461.html");

		HttpURLConnection con = (HttpURLConnection) url.openConnection();

		con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) ...");
		con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
		
		con.setRequestMethod("POST");
		con.setDoOutput(true);
		DataOutputStream wr =new DataOutputStream(con.getOutputStream());
		wr.writeBytes("wd=abcd");
		wr.flush();
		wr.close();
		
		readResponseBody(con.getInputStream());
	}

	public static void testGet() throws MalformedURLException, IOException {
		URL url = new URL("http://www.baidu.com");

		HttpURLConnection con = (HttpURLConnection) url.openConnection();

		con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) ...");
		con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

		int responseCode = con.getResponseCode();
		System.out.println(responseCode);

		String responseBody = readResponseBody(con.getInputStream());
	}

	// 读取输入流中的数据
	private static String readResponseBody(InputStream inputStream) throws IOException {

		BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
		String inputLine;
		StringBuffer response = new StringBuffer();

		while ((inputLine = in.readLine()) != null) {
			response.append(inputLine);
			System.out.println(inputLine);
		}
		in.close();

		return response.toString();
	}

}
